< previous page page_59 next page >

Page 59
arguments. The arguments expected by the API function. These may include data types defined by Type statements as described in the previous section.
type. The type returned by the API function.
For example, you could declare the CreateCompatibleBitmap function like this:
e10a6a0a59f035e870d90cca05b85fce.gif
Private Declare Function CreateCompatibleBitmap _
    Lib "gdi32" Alias "CreateCompatibleBitmap" ( _
    ByVal hdc As Long, ByVal nWidth As Long, _
    ByVal nHeight As Long) As Long
This function is in the gdi32 system library. It takes as parameters three long integers hDC, nWidth, and nHeightpassed by value. It returns a long integer.
You can use the API Viewer to copy subroutine and function declarations. Choose Declares from the API Type combo box to see a list of the function declarations available. Select the declarations you need and click the Add button to build a list of items for copying into your program. Click the Copy button to copy the declarations to the clipboard. Finally, use CTRL-V to paste the declarations from the clipboard into your Visual Basic project.
Argument Passing
By default, Visual Basic passes all variables by reference. That means Visual Basic passes the memory addresses of arguments rather than the argument values themselves. If you use the ByVal keyword in a Declare statement, or if you place ByVal in front of an argument when you call a routine, Visual Basic passes the argument by value rather than by reference.
Some API routines take arguments passed by value, and some take arguments passed by reference. Some routines even take some arguments by value and others by reference. When you call an API routine, you must be sure the arguments you pass to the routine exactly match the arguments the routine expects. Otherwise, you will be lucky if the routine merely fails to work properly. At worst, the routine will generate a General Protection Fault and crash your program, possibly taking Visual Basic and even Windows along for the ride. By using the ByVal keyword properly in Declare statements, you can ensure that Visual Basic passes arguments correctly.
The following sections explain some specific argument passing details.
Strings
Strings are a special case in argument passing. Internally, Visual Basic handles a string as the address of a data structure that contains the character data. Most API routines expect to receive the address of the character data rather than the address of the address of a data structure.
To tell Visual Basic to convert strings into addresses of character data, you should pass strings by value. Visual Basic does not actually pass the text value of the string. It passes the address of the characters.

 
< previous page page_59 next page >